package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 虎哥
 */
@Service
public class SpecService {

    @Autowired
    private SpecGroupMapper groupMapper;

    @Autowired
    private SpecParamMapper paramMapper;

    public List<SpecGroupDTO> queryGroupByCategoryId(Long id) {
        SpecGroup group = new SpecGroup();
        group.setCid(id);
        List<SpecGroup> list = groupMapper.select(group);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, SpecGroupDTO.class);
    }

    public List<SpecParamDTO> queryParams(Long gid, Long cid, Boolean searching) {
        // 过滤掉用户不传条件的情况
        if (cid == null && gid == null && searching == null) {
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
        // 构建查询条件
        SpecParam param = new SpecParam();
        param.setGroupId(gid);
        param.setCid(cid);
        param.setSearching(searching);
        // 查询数据
        List<SpecParam> list = paramMapper.select(param);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, SpecParamDTO.class);
    }

    public List<SpecGroupDTO> querySpecs(Long cid) {
        // 查询规格组
        List<SpecGroupDTO> groupDTOS = queryGroupByCategoryId(cid);

        // 查询分类下的所有参数
        List<SpecParamDTO> specParamDTOS = queryParams(null, cid, null);

        // 先对param按照groupId分组，形成一个Map,key是groupId,值是groupId一致的Param的集合
        Map<Long,List<SpecParamDTO>> map = specParamDTOS
                .stream()
                .collect(Collectors.groupingBy(SpecParamDTO::getGroupId));

        /*Map<Long,List<SpecParamDTO>> map = new HashMap<>();

        for (SpecParamDTO param : specParamDTOS) {
            Long groupId = param.getGroupId();
            // 判断param是否已经有分组了
            if (!map.containsKey(groupId)) {
                // 说明这个参数是改组的第一个，新增一个集合
                map.put(groupId, new ArrayList<>());
            }
            map.get(groupId).add(param);
        }*/




        // 遍历集合
        for (SpecGroupDTO group : groupDTOS) {
            // 给这一组创建一个集合，用来存放这一组的param
            group.setParams(map.get(group.getId()));
//            for (SpecParamDTO specParam : specParamDTOS) {
//                if(group.getId() == specParam.getGroupId()){
//                    group.getParams().add(specParam);
//                }
//            }
        }

        // 查询组内参数
//        for (SpecGroupDTO groupDTO : groupDTOS) {
//            List<SpecParamDTO> params = queryParams(groupDTO.getId(), null, null);
//            groupDTO.setParams(params);
//        }
        return groupDTOS;
    }
}
